const app = getApp()
const db = wx.cloud.database({
  env: 'gtss-2b1812'
})
Page({
  data: {
    gjz: '',
    type: '',
    listStu: [],
    listTea: []
  },
  onLoad: function(e) {
    this.setData({
      type: e.type
    })
  },
  onShow: function() {
    const that = this
    if (that.data.type == 'stu') {
      db.collection('Student').field({
        _id: true,
        stuName: true
      }).where({
        stuDept: app.globalData.userDept
      }).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listStu: res.data
        })
      })
    }
    if (that.data.type == 'tea') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true
      }).where({
        teaDept: app.globalData.userDept
      }).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listTea: res.data
        })
      })
    }
  },
  onReachBottom: function() {
    const that = this
    const _ = db.command
    if (that.data.gjz == '' && that.data.type == 'stu') {
      db.collection('Student').field({
        _id: true,
        stuName: true
      }).where({
        stuDept: app.globalData.userDept
        }).skip(that.data.listStu.length).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listStu: that.data.listStu.concat(res.data)
        })
      })
    }
    if (that.data.gjz == '' && that.data.type == 'tea') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true
      }).where({
        teaDept: app.globalData.userDept
        }).skip(that.data.listTea.length).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listTea: that.data.listTea.concat(res.data)
        })
      })
    }
    if (that.data.gjz != '' && that.data.type == 'stu') {
      db.collection('Student').field({
        _id: true,
        stuName: true
      }).where(
        _.or([{
            stuName: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          },
          {
            _id: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          }
        ])).where({
        stuDept: app.globalData.userDept
      }).skip(that.data.listStu.length).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listStu: that.data.listStu.concat(res.data)
        })
      })
    }
    if (that.data.gjz != '' && that.data.type == 'tea') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true
      }).where(
        _.or([{
            teaName: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          },
          {
            _id: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          }
        ])).where({
        teaDept: app.globalData.userDept
      }).skip(that.data.listTea.length).limit(20).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listTea: that.data.listTea.concat(res.data)
        })
      })
    }
  },
  inpGjz: function(e) {
    const that = this
    that.setData({
      gjz: e.detail.value
    })
    if (e.detail.value == '' && that.data.type == 'tea') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true
      }).limit(20).where({
        teaDept: app.globalData.userDept
      }).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listTea: res.data
        })
      })
    }
    if (e.detail.value == '' && that.data.type == 'stu') {
      db.collection('Student').field({
        _id: true,
        stuName: true
      }).limit(20).where({
        stuDept: app.globalData.userDept
      }).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listStu: res.data
        })
      })
    }
  },
  search: function() {
    const that = this
    const _ = db.command
    if (that.data.type == 'tea') {
      db.collection('Teacher').field({
        _id: true,
        teaName: true
      }).limit(20).where(
        _.or([{
            teaName: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          },
          {
            _id: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          }
        ])).where({
        teaDept: app.globalData.userDept
      }).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listTea: res.data
        })
      })
    }
    if (that.data.type == 'stu') {
      db.collection('Student').field({
        _id: true,
        stuName: true
      }).limit(20).where(
        _.or([{
            stuName: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          },
          {
            _id: new db.RegExp({
              regexp: that.data.gjz,
              options: 'i',
            })
          }
        ])).where({
        stuDept: app.globalData.userDept
      }).orderBy('_id', 'asc').get().then(res => {
        that.setData({
          listStu: res.data
        })
      })
    }
  },
  funAction: function(e) {
    wx.navigateTo({
      url: '../admAction/admAction?type=' + this.data.type + '&id=' + e.currentTarget.dataset.id
    })
  },
  funAddUser: function() {
    wx.navigateTo({
      url: '../admAddUser/admAddUser?type=' + this.data.type
    })
  }
})